import 'package:happy_study/core/model/student_model.dart';
import 'package:happy_study/core/sqlite_database/util/db_provider.dart';
import 'package:sqflite/sqflite.dart';

class StudentDao extends DBProvider {
  final String name = "student";
  final String columnId = "_id";

  @override
  tableName() {
    return name;
  }

  @override
  tableSqlString() {
    return tableBaseString(name, columnId) +
        '''
      studentId text,
      studentName text,
      studentLevel integer,
      studentExperience integer)
    ''';
  }

  ///插入数据
  Future insert(student) async {
    Database db = await getDB();
    return await db.insert(name, StudentModel.toMap(student));
  }

  ///搜索数据
  Future<List<StudentModel>> queryAll() async {
    List<StudentModel> students = [];
    Database db = await getDB();
    var res = await db.query(name);
    for (var json in res) {
      students.add(StudentModel.fromJson(json));
    }
    return students;
  }

  Future updateStudent(StudentModel student) async {
    Database db = await getDB();
    await db.update(
      name,
      StudentModel.toMap(student),
      where: 'studentId = ?',
      whereArgs: [student.studentId],
    );
  }
}
